package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.*;
import com.antdesignbackend.backend.entity.entityVo.LogVo;
import com.antdesignbackend.backend.service.IDictionaryService;
import com.antdesignbackend.backend.service.ILogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/log")
public class LogController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    private ILogService logService;
    @Autowired
    private IDictionaryService dictionaryService;

    //也可以直接注入mapper来进行操作
    // @Autowired
    //userDemoMapper userDemoMapper;
    /**
     * 操作日志 index
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        // ModelAndView modelAndView = new ModelAndView("client/index");
        return "log/index";

    }
    /**
     * 系统日志 index
     */
    @GetMapping("/sysIndex")
    @AuthToken
    public String sysIndex() {
        // ModelAndView modelAndView = new ModelAndView("client/index");
        return "log/sysIndex";

    }


    /***
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        result = new ReturnVO();
        if (logService.removeById(Integer.parseInt(id.toString())) == true) {
            result.setMessage("删除成功");
        } else {
            result.setMessage("删除失败");
            result.setCode("0");
        }
        return result;
    }
    /***
     * 前端分页查询
     *
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody LogVo data) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        result = new ReturnVO();
        List<LogVo> datalist=new ArrayList<>();
        QueryWrapper<LogVo> wrapper =new QueryWrapper<LogVo>();
        if(data.getStartTime()!=null&& !data.getStartTime().equals(""))
        {
            wrapper.and(wra -> wra.ge("t.createTime",data.getStartTime()));
        }
        if(data.getEndTime()!=null&& !data.getEndTime().equals(""))
        {
            wrapper.and(wra -> wra.le("t.createTime",data.getEndTime()));
        }

        if(data.getLogType()!=null&& !data.getLogType().equals(""))
        {
            wrapper.eq("t.logType",data.getLogType());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=logService.count(wrapper);
        result.setTotal(total);
        //再查询
        if(total>=0)
        {
            datalist= logService.search(wrapper.orderByDesc("t.createTime"),new Page<>(data.getPageIndex(),data.getPageSize()));

            //得到当前菜单具备的功能权限
//            List<Button> list = permissionService.filterPermission(String.valueOf(roleID), menuId);
//            datalist.forEach(model -> model.setButtonList(list));
        }
        result.setData(datalist);
        return result;


    }
    /**
     * 获取字典表
     * 用于绑定下拉框
     */
    @ResponseBody
    @GetMapping(value = "/getDictionary")
    public List<Dictionary> getDictionary(String logKey) {
        List<Dictionary> dataList = new ArrayList<>();
        QueryWrapper<Dictionary> wrapper =new QueryWrapper<Dictionary>();
        wrapper.eq("enable",1);
        wrapper.eq("type",logKey);
        dataList = dictionaryService.getBaseMapper().selectList(wrapper);
        return dataList;
    }


}
